<?php
if ( ! function_exists( 'get_custom_header' ) ) {
	// compatibility with versions of WordPress prior to 3.4.
	add_custom_background();
} else {
	add_theme_support( 'custom-background', apply_filters( 'realestate_custom_background_args', array() ) );
}

if (function_exists('add_post_type_support')) add_post_type_support( 'page', 'excerpt' );
add_theme_support( 'automatic-feed-links' );

add_action('init','realestate_activate_features');
function realestate_activate_features(){
	/* activate shortcodes */
	require_once(TEMPLATEPATH . '/epanel/shortcodes/shortcodes.php');

	/* activate page templates */
	require_once(TEMPLATEPATH . '/epanel/page_templates/page_templates.php');

}

add_filter('widget_text', 'do_shortcode');

if ( ! function_exists( 'realestate_options_stored_in_one_row' ) ){
	function realestate_options_stored_in_one_row(){
		global $realestate_store_options_in_one_row;
		return isset( $realestate_store_options_in_one_row ) ? (bool) $realestate_store_options_in_one_row : false;
	}
}

if ( ! function_exists( 'realestate_get_option' ) ){
	function realestate_get_option( $option_name, $default_value = '' ){
		global $realestate_theme_options, $shortname;

		if ( realestate_options_stored_in_one_row() ){
			$realestate_theme_options_name = 'realestate_' . $shortname;

			if ( ! isset( $realestate_theme_options ) ) $realestate_theme_options = get_option( $realestate_theme_options_name );
			$option_value = isset ( $realestate_theme_options[$option_name] ) ? $realestate_theme_options[$option_name] : false;
		} else {
			$option_value = get_option( $option_name );
		}

		if ( !$option_value && '' != $default_value ) $option_value = $default_value;

		return $option_value;
	}
}

if ( ! function_exists( 'realestate_update_option' ) ){
	function realestate_update_option( $option_name, $new_value ){
		global $realestate_theme_options, $shortname;

		if ( realestate_options_stored_in_one_row() ){
			$realestate_theme_options_name = 'realestate_' . $shortname;

			if ( ! isset( $realestate_theme_options ) ) $realestate_theme_options = get_option( $realestate_theme_options_name );
			$realestate_theme_options[$option_name] = $new_value;

			$option_name = $realestate_theme_options_name;
			$new_value = $realestate_theme_options;
		}

		update_option( $option_name, $new_value );
	}
}

if ( ! function_exists( 'realestate_delete_option' ) ){
	function realestate_delete_option( $option_name ){
		global $realestate_theme_options, $shortname;

		if ( realestate_options_stored_in_one_row() ){
			$realestate_theme_options_name = 'realestate_' . $shortname;

			if ( ! isset( $realestate_theme_options ) ) $realestate_theme_options = get_option( $realestate_theme_options_name );

			unset( $realestate_theme_options[$option_name] );
			update_option( $realestate_theme_options_name, $realestate_theme_options );
		} else {
			delete_option( $option_name );
		}
	}
}

add_filter('body_class', 'realestate_browser_body_class');
function realestate_browser_body_class($classes) {
	global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

	if($is_lynx) $classes[] = 'lynx';
	elseif($is_gecko) $classes[] = 'gecko';
	elseif($is_opera) $classes[] = 'opera';
	elseif($is_NS4) $classes[] = 'ns4';
	elseif($is_safari) $classes[] = 'safari';
	elseif($is_chrome) $classes[] = 'chrome';
	elseif($is_IE) $classes[] = 'ie';
	else $classes[] = 'unknown';

	if($is_iphone) $classes[] = 'iphone';
	return $classes;
}

/*this function allows for the auto-creation of post excerpts*/
if ( ! function_exists( 'truncate_post' ) ){
	function truncate_post($amount, $echo = true, $post='') {
		global $shortname;

		if ( $post == '' ) global $post;

		$postExcerpt = '';
		$postExcerpt = $post->post_excerpt;

		if (realestate_get_option($shortname.'_use_excerpt') == 'on' && $postExcerpt <> '') {
			if ($echo) echo $postExcerpt;
			else return $postExcerpt;
		} else {
			$truncate = $post->post_content;

			$truncate = preg_replace('@\[caption[^\]]*?\].*?\[\/caption]@si', '', $truncate);

			if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...';
			$truncate = apply_filters('the_content', $truncate);
			$truncate = preg_replace('@<script[^>]*?>.*?</script>@si', '', $truncate);
			$truncate = preg_replace('@<style[^>]*?>.*?</style>@si', '', $truncate);

			$truncate = strip_tags($truncate);

			if ($echo_out == '...') $truncate = substr($truncate, 0, strrpos(substr($truncate, 0, $amount), ' '));
			else $truncate = substr($truncate, 0, $amount);

			if ($echo) echo $truncate,$echo_out;
			else return ($truncate . $echo_out);
		};
	}
}

/*this function truncates titles to create preview excerpts*/
if ( ! function_exists( 'truncate_title' ) ){
	function truncate_title($amount, $echo = true, $post = '') {
		if ( $post == '' ) $truncate = get_the_title();
		else $truncate = $post->post_title;
		if ( strlen($truncate) <= $amount ) $echo_out = ''; else $echo_out = '...';
		$truncate = mb_substr( $truncate, 0, $amount, 'UTF-8' );
		if ($echo) {
			echo $truncate;
			echo $echo_out;
		}
		else
		{
			return ($truncate . $echo_out);
		}
	}
}

/*this function allows users to use the first image in their post as their thumbnail*/
if ( ! function_exists( 'realestate_first_image' ) ){
	function realestate_first_image() {
		global $post;
		$img = '';
		$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches);
		if ( isset($matches[1][0]) ) $img = $matches[1][0];
		return trim($img);
	}
}

/* this function gets thumbnail from Post Thumbnail or Custom field or First post image */
if ( ! function_exists( 'get_thumbnail' ) ){
	function get_thumbnail($width = 100, $height = 100, $class = '', $alttext = '', $titletext = '', $fullpath = false, $custom_field = '', $post = '')
	{
		if ( $post == '' ) global $post;
		global $shortname;

		$thumb_array['thumb'] = '';
		$thumb_array['use_timthumb'] = true;
		if ($fullpath) $thumb_array['fullpath'] = ''; //full image url for lightbox

		$new_method = true;

		if ( has_post_thumbnail( $post->ID ) && !( '' != $custom_field && get_post_meta( $post->ID, $custom_field, true ) ) ) {
			$thumb_array['use_timthumb'] = false;

			$realestate_fullpath =  wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
			$thumb_array['fullpath'] =  $realestate_fullpath[0];
			$thumb_array['thumb'] = $thumb_array['fullpath'];
		}

		if ($thumb_array['thumb'] == '') {
			if ($custom_field == '') $thumb_array['thumb'] = esc_attr( get_post_meta($post->ID, 'Thumbnail', $single = true) );
			else {
				$thumb_array['thumb'] = esc_attr( get_post_meta($post->ID, $custom_field, $single = true) );
				if ($thumb_array['thumb'] == '') $thumb_array['thumb'] = esc_attr( get_post_meta($post->ID, 'Thumbnail', $single = true) );
			}

			if (($thumb_array['thumb'] == '') && ((realestate_get_option($shortname.'_grab_image')) == 'on')) {
				$thumb_array['thumb'] = esc_attr( realestate_first_image() );
				if ( $fullpath ) $thumb_array['fullpath'] = $thumb_array['thumb'];
			}

			#if custom field used for small pre-cropped image, open Thumbnail custom field image in lightbox
			if ($fullpath) {
				$thumb_array['fullpath'] = $thumb_array['thumb'];
				if ($custom_field == '') $thumb_array['fullpath'] = apply_filters('realestate_fullpath', realestate_path_reltoabs(esc_attr($thumb_array['thumb'])));
				elseif ( $custom_field <> '' && get_post_meta($post->ID, 'Thumbnail', $single = true) ) $thumb_array['fullpath'] = apply_filters( 'realestate_fullpath', realestate_path_reltoabs(esc_attr(get_post_meta($post->ID, 'Thumbnail', $single = true))) );
			}
		}

		return $thumb_array;
	}
}

/* this function prints thumbnail from Post Thumbnail or Custom field or First post image */
if ( ! function_exists( 'print_thumbnail' ) ){
	function print_thumbnail($thumbnail = '', $use_timthumb = true, $alttext = '', $width = 100, $height = 100, $class = '', $echoout = true, $forstyle = false, $resize = true, $post = '') {
		global $shortname;
		if ( $post == '' ) global $post;

		$output = '';
		$thumbnail_orig = $thumbnail;

		$thumbnail = realestate_multisite_thumbnail( $thumbnail );

		$cropPosition = '';

		$allow_new_thumb_method = false;

		$new_method = true;
		$new_method_thumb = '';
		$external_source = false;

		$allow_new_thumb_method = !$external_source && $new_method && $cropPosition == '';

		if ( $allow_new_thumb_method && $thumbnail <> '' ){
			$realestate_crop = get_post_meta( $post->ID, 'realestate_nocrop', true ) == '' ? true : false;
			$new_method_thumb =  realestate_resize_image( realestate_path_reltoabs($thumbnail), $width, $height, $realestate_crop );
			if ( is_wp_error( $new_method_thumb ) ) $new_method_thumb = '';
		}

		if ($forstyle === false) {
			$output = '<img src="' . esc_url( $new_method_thumb ) . '"';

			if ($class <> '') $output .= " class='" . esc_attr( $class ) . "' ";

			$dimensions = apply_filters( 'realestate_print_thumbnail_dimensions', " width='" . esc_attr( $width ) . "' height='" .esc_attr( $height ) . "'" );

			$output .= " alt='" . esc_attr( strip_tags( $alttext ) ) . "'{$dimensions} />";

			if (!$resize) $output = $thumbnail;
		} else {
			$output = $new_method_thumb;
		}

		if ($echoout) echo $output;
		else return $output;
	}
}

if ( ! function_exists( 'realestate_new_thumb_resize' ) ){
	function realestate_new_thumb_resize( $thumbnail, $width, $height, $alt='', $forstyle = false ){
		global $shortname;

		$new_method = true;
		$new_method_thumb = '';
		$external_source = false;

		$allow_new_thumb_method = !$external_source && $new_method;

		if ( $allow_new_thumb_method && $thumbnail <> '' ){
			$realestate_crop = true;
			$new_method_thumb = realestate_resize_image( $thumbnail, $width, $height, $realestate_crop );
			if ( is_wp_error( $new_method_thumb ) ) $new_method_thumb = '';
		}

		$thumb = esc_attr( $new_method_thumb );

		$output = '<img src="' . esc_url( $thumb ) . '" alt="' . esc_attr( $alt ) . '" width =' . esc_attr( $width ) . ' height=' . esc_attr( $height ) . ' />';

		return ( !$forstyle ) ? $output : $thumb;
	}
}

if ( ! function_exists( 'realestate_multisite_thumbnail' ) ){
	function realestate_multisite_thumbnail( $thumbnail = '' ) {
		// do nothing if it's not a Multisite installation or current site is the main one
		if ( is_main_site() ) return $thumbnail;

		# get the real image url
		preg_match( '#([_0-9a-zA-Z-]+/)?files/(.+)#', $thumbnail, $matches );
		if ( isset( $matches[2] ) ){
			$file = rtrim( BLOGUPLOADDIR, '/' ) . '/' . str_replace( '..', '', $matches[2] );
			if ( is_file( $file ) ) $thumbnail = str_replace( ABSPATH, trailingslashit( get_site_url( 1 ) ), $file );
			else $thumbnail = '';
		}

		return $thumbnail;
	}
}

if ( ! function_exists( 'realestate_is_portrait' ) ){
	function realestate_is_portrait($imageurl, $post='', $ignore_cfields = false){
		if ( $post == '' ) global $post;

		if ( get_post_meta($post->ID, 'realestate_disable_portrait', true) == 1 ) return false;

		if ( !$ignore_cfields ) {
			if ( get_post_meta($post->ID,'realestate_imagetype',true) == 'l' ) return false;
			if ( get_post_meta($post->ID,'realestate_imagetype',true) == 'p' ) return true;
		}

		$imageurl = realestate_path_reltoabs(realestate_multisite_thumbnail($imageurl));

		$realestate_thumb_size = @getimagesize($imageurl);
		if ( empty($realestate_thumb_size) ) {
			$realestate_thumb_size = @getimagesize( str_replace( WP_CONTENT_URL, WP_CONTENT_DIR, $imageurl ) );
			if ( empty($realestate_thumb_size) ) return false;
		}
		$realestate_thumb_width = $realestate_thumb_size[0];
		$realestate_thumb_height = $realestate_thumb_size[1];

		$result = ($realestate_thumb_width < $realestate_thumb_height) ? true : false;

		return $result;
	}
}

if ( ! function_exists( 'realestate_path_reltoabs' ) ){
	function realestate_path_reltoabs( $imageurl ){
		if ( strpos(strtolower($imageurl), 'http://') !== false || strpos(strtolower($imageurl), 'https://') !== false ) return $imageurl;

		if ( strpos( strtolower($imageurl), $_SERVER['HTTP_HOST'] ) !== false )
			return $imageurl;
		else {
			$imageurl = apply_filters( 'realestate_path_relative_image', site_url() . '/' ) . $imageurl;
		}

		return $imageurl;
	}
}

if ( ! function_exists( 'in_subcat' ) ){
	function in_subcat($blogcat,$current_cat='') {
		$in_subcategory = false;

		if (cat_is_ancestor_of($blogcat,$current_cat) || $blogcat == $current_cat) $in_subcategory = true;

		return $in_subcategory;
	}
}

if ( ! function_exists( 'show_page_menu' ) ){
	function show_page_menu($customClass = 'nav clearfix', $addUlContainer = true, $addHomeLink = true){
		global $shortname, $themename, $exclude_pages, $strdepth, $page_menu, $is_footer;

		//excluded pages
		if (realestate_get_option($shortname.'_menupages') <> '') $exclude_pages = implode(',', realestate_get_option($shortname.'_menupages'));

		//dropdown for pages
		$strdepth = '';
		if (realestate_get_option($shortname.'_enable_dropdowns') == 'on') $strdepth = "depth=".realestate_get_option($shortname.'_tiers_shown_pages');
		if ($strdepth == '') $strdepth = "depth=1";

		if ($is_footer) { $strdepth = "depth=1"; $strdepth2 = $strdepth; }

		$page_menu = wp_list_pages('sort_column='.realestate_get_option($shortname.'_sort_pages').'&sort_order='.realestate_get_option($shortname.'_order_page').'&'.$strdepth.'exclude='.$exclude_pages.'&title_li=&echo=0');

		if ($addUlContainer) echo('<ul class="'.$customClass.'">');
			if (realestate_get_option($shortname . '_home_link') == 'on' && $addHomeLink) { ?>
				<li <?php if (is_front_page() || is_home()) echo('class="current_page_item"') ?>><a href="<?php echo esc_url( home_url() ); ?>"><?php _e('Home',$themename); ?></a></li>
			<?php };

			echo $page_menu;
		if ($addUlContainer) echo('</ul>');
	}
}

if ( ! function_exists( 'show_categories_menu' ) ){
	function show_categories_menu($customClass = 'nav clearfix', $addUlContainer = true){
		global $shortname, $themename, $category_menu, $exclude_cats, $hide, $strdepth2, $projects_cat;

		//excluded categories
		if (realestate_get_option($shortname.'_menucats') <> '') $exclude_cats = implode(',', realestate_get_option($shortname.'_menucats'));

		//hide empty categories
		if (realestate_get_option($shortname.'_categories_empty') == 'on') $hide = '1';
		else $hide = '0';

		//dropdown for categories
		$strdepth2 = '';
		if (realestate_get_option($shortname.'_enable_dropdowns_categories') == 'on') $strdepth2 = "depth=".realestate_get_option($shortname.'_tiers_shown_categories');
		if ($strdepth2 == '') $strdepth2 = "depth=1";

		$args = "orderby=".realestate_get_option($shortname.'_sort_cat')."&order=".realestate_get_option($shortname.'_order_cat')."&".$strdepth2."&exclude=".$exclude_cats."&hide_empty=".$hide."&title_li=&echo=0";

		$categories = get_categories( $args );

		if ( !empty($categories) ) {
			$category_menu = wp_list_categories($args);
			if ($addUlContainer) echo('<ul class="'.$customClass.'">');
				echo $category_menu;
			if ($addUlContainer) echo('</ul>');
		}
	}
}

function head_addons(){
	global $shortname, $default_colorscheme;

	if ( apply_filters('realestate_get_additional_color_scheme', realestate_get_option($shortname.'_color_scheme')) <> $default_colorscheme ) { ?>
		<link rel="stylesheet" href="<?php echo esc_url(get_template_directory_uri() . '/style-' . realestate_get_option($shortname.'_color_scheme') . '.css' ); ?>" type="text/css" media="screen" />
	<?php };

	if (realestate_get_option($shortname.'_child_css') == 'on' && realestate_get_option($shortname.'_child_cssurl') <> '' ) { //Enable child stylesheet  ?>
		<link rel="stylesheet" href="<?php echo esc_url( realestate_get_option($shortname.'_child_cssurl') ); ?>" type="text/css" media="screen" />
	<?php };

	//prints the theme name, version in meta tag
	if ( ! function_exists( 'get_custom_header' ) ){
		// compatibility with versions of WordPress prior to 3.4.
		$theme_info = get_theme_data(TEMPLATEPATH . '/style.css');
		echo '<meta content="' . esc_attr( $theme_info['Name'] . ' v.' . $theme_info['Version'] ) . '" name="generator"/>';
	} else {
		$theme_info = wp_get_theme();
		echo '<meta content="' . esc_attr( $theme_info->display('Name') . ' v.' . $theme_info->display('Version') ) . '" name="generator"/>';
	}

	if (realestate_get_option($shortname.'_custom_colors') == 'on') custom_colors_css();

};// end function head_addons()
add_action('wp_head','head_addons',7);

function integration_head(){
	global $shortname;
	if (realestate_get_option($shortname.'_integration_head') <> '' && realestate_get_option($shortname.'_integrate_header_enable') == 'on') echo( realestate_get_option($shortname.'_integration_head') );
};
add_action('wp_head','integration_head',12);

function integration_body(){
	global $shortname;
	if (realestate_get_option($shortname.'_integration_body') <> '' && realestate_get_option($shortname.'_integrate_body_enable') == 'on') echo( realestate_get_option($shortname.'_integration_body') );
};
add_action('wp_footer','integration_body',12);

/*this function gets page name by its id*/
if ( ! function_exists( 'get_pagename' ) ){
	function get_pagename( $page_id )
	{
		$page_object = get_page( $page_id );
		return apply_filters( 'the_title', $page_object->post_title, $page_id );
	}
}

/*this function gets category name by its id*/
if ( !function_exists( 'realestate_category_name' ) ){
	function realestate_category_name( $cat_id )
	{
		return get_cat_name( $cat_id );
	}
}

/*this function gets category id by its name*/
if ( ! function_exists( 'get_catId' ) ){
	function get_catId($cat_name)
	{
		$cat_name_id = get_cat_ID( html_entity_decode( $cat_name, ENT_QUOTES ) );
		return $cat_name_id;
	}
}

/*this function gets page id by its name*/
if ( ! function_exists( 'get_pageId' ) ){
	function get_pageId( $page_name )
	{
		global $wpdb;

		if ( is_numeric( $page_name ) ) return $page_name;

		$page_name = html_entity_decode( $page_name, ENT_QUOTES );
		$page = get_page_by_title( $page_name );

		//fix for qtranslate plugin
		if ( ! $page ){
			$qt_page_name = '%<!--:en-->' . $page_name . '<!--:-->%';
			$qt_page_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_title LIKE %s AND post_type= %s", $qt_page_name, 'page' ) );

			if ( $qt_page_id ) return $qt_page_id;
			else return null;
		}

		return $page->ID;
	}
}

/*this function controls the meta titles display*/
if ( ! function_exists( 'realestate_titles' ) ){
	function realestate_titles() {
		global $shortname;

		$sitename = get_bloginfo('name');
		$site_description = get_bloginfo('description');

		#if the title is being displayed on the homepage
		if (is_home() || is_front_page()) {
			if (realestate_get_option($shortname.'_seo_home_title') == 'on') echo realestate_get_option($shortname.'_seo_home_titletext');
			else {
				$seo_home_type = realestate_get_option( $shortname . '_seo_home_type' );
				$seo_home_separate = realestate_get_option($shortname.'_seo_home_separate');

				if ( $seo_home_type == 'BlogName | Blog description' ) echo $sitename . $seo_home_separate . $site_description;
				if ( $seo_home_type == 'Blog description | BlogName') echo $site_description. $seo_home_separate . $sitename;
				if ( $seo_home_type == 'BlogName only') echo $sitename;
			}
		}
		#if the title is being displayed on single posts/pages
		if (is_single() || is_page()) {
			global $wp_query;
			$postid = $wp_query->post->ID;
			$key = realestate_get_option($shortname.'_seo_single_field_title');
			$exists3 = get_post_meta($postid, ''.$key.'', true);
					if (realestate_get_option($shortname.'_seo_single_title') == 'on' && $exists3 !== '' ) echo $exists3;
					else {
						$seo_single_type = realestate_get_option($shortname.'_seo_single_type');
						$seo_single_separate = realestate_get_option($shortname.'_seo_single_separate');
						if ( $seo_single_type == 'BlogName | Post title' ) echo $sitename . $seo_single_separate . wp_title('',false,'');
						if ( $seo_single_type == 'Post title | BlogName' ) echo wp_title('',false,'') . $seo_single_separate . $sitename;
						if ( $seo_single_type == 'Post title only' ) echo wp_title('',false,'');
					}
		}
		#if the title is being displayed on index pages (categories/archives/search results)
		if (is_category() || is_archive() || is_search()) {
			$seo_index_type = realestate_get_option($shortname.'_seo_index_type');
			$seo_index_separate = realestate_get_option($shortname.'_seo_index_separate');
			if ( $seo_index_type == 'BlogName | Category name' ) echo $sitename . $seo_index_separate . wp_title('',false,'');
			if ( $seo_index_type == 'Category name | BlogName') echo wp_title('',false,'') . $seo_index_separate . $sitename;
			if ( $seo_index_type == 'Category name only') echo wp_title('',false,'');
		}
	}
}

/*this function controls the meta description display*/
if ( ! function_exists( 'realestate_description' ) ){
	function realestate_description() {
		global $shortname;

		#homepage descriptions
		if ( is_home() && realestate_get_option($shortname.'_seo_home_description') == 'on' ) echo '<meta name="description" content="' . esc_attr( realestate_get_option($shortname.'_seo_home_descriptiontext') ) .'" />';

		#single page descriptions
		global $wp_query;
		if ( isset($wp_query->post->ID) ) $postid = $wp_query->post->ID;
		$key2 = realestate_get_option($shortname.'_seo_single_field_description');
		if ( isset($postid) ) $exists = get_post_meta($postid, ''.$key2.'', true);
		if (realestate_get_option($shortname.'_seo_single_description') == 'on' && $exists !== '') {
			if (is_single() || is_page()) echo '<meta name="description" content="' . esc_attr( $exists ) . '" />';
		}

		#index descriptions
		remove_filter('term_description','wpautop');
		$cat = get_query_var('cat');
		$exists2 = category_description($cat);

		$seo_index_description = realestate_get_option($shortname.'_seo_index_description');

		if ($exists2 !== '' && $seo_index_description == 'on') {
			if (is_category()) echo '<meta name="description" content="'. esc_attr( $exists2 ) .'" />';
		}
		if (is_archive() && $seo_index_description == 'on') echo '<meta name="description" content="Currently viewing archives from'. esc_attr( wp_title('',false,'') ) .'" />';
		if (is_search() && $seo_index_description == 'on') echo '<meta name="description" content="'. esc_attr( wp_title('',false,'') ) .'" />';
	}
}

/*this function controls the meta keywords display*/
if ( ! function_exists( 'realestate_keywords' ) ){
	function realestate_keywords() {
		global $shortname;

		#homepage keywords
		if (is_home() && realestate_get_option($shortname.'_seo_home_keywords') == 'on') echo '<meta name="keywords" content="'.esc_attr( realestate_get_option($shortname.'_seo_home_keywordstext') ).'" />';

		#single page keywords
		global $wp_query;
		if (isset($wp_query->post->ID)) $postid = $wp_query->post->ID;
		$key3 = realestate_get_option($shortname.'_seo_single_field_keywords');
		if (isset($postid)) $exists4 = get_post_meta($postid, ''.$key3.'', true);
		if (isset($exists4) && $exists4 !== '' && realestate_get_option($shortname.'_seo_single_keywords') == 'on') {
			if (is_single() || is_page()) echo '<meta name="keywords" content="' . esc_attr( $exists4 ) . '" />';
		}
	}
}

/*this function controls canonical urls*/
if ( ! function_exists( 'realestate_canonical' ) ){
	function realestate_canonical() {
		global $shortname;

		#homepage urls
		if (is_home() && realestate_get_option($shortname.'_seo_home_canonical') == 'on') echo '<link rel="canonical" href="'. esc_url( home_url() ).'" />';

		#single page urls
		global $wp_query;
		if (isset($wp_query->post->ID)) $postid = $wp_query->post->ID;
		if (realestate_get_option($shortname.'_seo_single_canonical') == 'on') {
			if (is_single() || is_page()) echo '<link rel="canonical" href="'.esc_url( get_permalink() ).'" />';
		}

		#index page urls
		if (realestate_get_option($shortname.'_seo_index_canonical') == 'on') {
			if (is_archive() || is_category() || is_search()) echo '<link rel="canonical" href="'. esc_url( get_permalink() ).'" />';
		}
	}
}

add_action('wp_head','add_favicon');
function add_favicon(){
	global $shortname;

	$faviconUrl = realestate_get_option($shortname.'_favicon');
	if ($faviconUrl <> '') echo('<link rel="shortcut icon" href="'.esc_url( $faviconUrl ).'" />');
}

add_action( 'init', 'realestate_create_images_temp_folder' );
function realestate_create_images_temp_folder(){
	#clean realestate_temp folder once per week
	if ( false !== $last_time = get_option( 'realestate_schedule_clean_images_last_time'  ) ){
		$timeout = 86400 * 7;
		if ( ( $timeout < ( time() - $last_time ) ) && '' != get_option( 'realestate_images_temp_folder' ) ) realestate_clean_temp_images( get_option( 'realestate_images_temp_folder' ) );
	}

	if ( false !== get_option( 'realestate_images_temp_folder' ) ) return;

	$uploads_dir = wp_upload_dir();
	$destination_dir = ( false === $uploads_dir['error'] ) ? path_join( $uploads_dir['basedir'], 'realestate_temp' ) : null;

	if ( ! wp_mkdir_p( $destination_dir ) ) update_option( 'realestate_images_temp_folder', '' );
	else {
		update_option( 'realestate_images_temp_folder', preg_replace( '#\/\/#', '/', $destination_dir ) );
		update_option( 'realestate_schedule_clean_images_last_time', time() );
	}
}

if ( ! function_exists( 'realestate_clean_temp_images' ) ){
	function realestate_clean_temp_images( $directory ){
		$dir_to_clean = @ opendir( $directory );

		if ( $dir_to_clean ) {
			while (($file = readdir( $dir_to_clean ) ) !== false ) {
				if ( substr($file, 0, 1) == '.' )
					continue;
				if ( is_dir( $directory.'/'.$file ) )
					realestate_clean_temp_images( path_join( $directory, $file ) );
				else
					@unlink( path_join( $directory, $file ) );
			}
			closedir( $dir_to_clean );
		}

		#set last time cleaning was performed
		update_option( 'realestate_schedule_clean_images_last_time', time() );
	}
}

add_filter( 'update_option_upload_path', 'realestate_update_uploads_dir' );
function realestate_update_uploads_dir( $upload_path ){
	#check if we have 'realestate_temp' folder within $uploads_dir['basedir'] directory, if not - try creating it, if it's not possible $destination_dir = null

	$destination_dir = '';
	$uploads_dir = wp_upload_dir();
	$realestate_temp_dir = path_join( $uploads_dir['basedir'], 'realestate_temp' );

	if ( is_dir( $realestate_temp_dir ) || ( false === $uploads_dir['error'] && wp_mkdir_p( $realestate_temp_dir ) ) ){
		$destination_dir = $realestate_temp_dir;
		update_option( 'realestate_schedule_clean_images_last_time', time() );
	}

	update_option( 'realestate_images_temp_folder', preg_replace( '#\/\/#', '/', $destination_dir ) );

	return $upload_path;
}

if ( ! function_exists( 'realestate_resize_image' ) ){
	function realestate_resize_image( $thumb, $new_width, $new_height, $crop ){
		if ( is_ssl() ) $thumb = preg_replace( '#^http://#', 'https://', $thumb );
		$info = pathinfo($thumb);
		$ext = $info['extension'];
		$name = wp_basename($thumb, ".$ext");
		$is_jpeg = false;
		$site_uri = apply_filters( 'realestate_resize_image_site_uri', site_url() );
		$site_dir = apply_filters( 'realestate_resize_image_site_dir', ABSPATH );

		#get main site url on multisite installation 
		if ( is_multisite() ){
			switch_to_blog(1);
			$site_uri = site_url();
			restore_current_blog();
		}

		if ( 'jpeg' == $ext ) {
			$ext = 'jpg';
			$name = preg_replace( '#.jpeg$#', '', $name );
			$is_jpeg = true;
		}

		$suffix = "{$new_width}x{$new_height}";

		$destination_dir = '' != get_option( 'realestate_images_temp_folder' ) ? preg_replace( '#\/\/#', '/', get_option( 'realestate_images_temp_folder' ) ) : null;

		$matches = apply_filters( 'realestate_resize_image_site_dir', array(), $site_dir );
		if ( !empty($matches) ){
			preg_match( '#'.$matches[1].'$#', $site_uri, $site_uri_matches );
			if ( !empty($site_uri_matches) ){
				$site_uri = str_replace( $matches[1], '', $site_uri );
				$site_uri = preg_replace( '#/$#', '', $site_uri );
				$site_dir = str_replace( $matches[1], '', $site_dir );
				$site_dir = preg_replace( '#\\\/$#', '', $site_dir );
			}
		}

		#get local name for use in file_exists() and get_imagesize() functions
		$localfile = str_replace( apply_filters( 'realestate_resize_image_localfile', $site_uri, $site_dir, realestate_multisite_thumbnail($thumb) ), $site_dir, realestate_multisite_thumbnail($thumb) );

		$add_to_suffix = '';
		if ( file_exists( $localfile ) ) $add_to_suffix = filesize( $localfile ) . '_';

		#prepend image filesize to be able to use images with the same filename
		$suffix = $add_to_suffix . $suffix;
		$destfilename_attributes = '-' . $suffix . '.' . $ext;

		$checkfilename = ( '' != $destination_dir && null !== $destination_dir ) ? path_join( $destination_dir, $name ) : path_join( dirname( $localfile ), $name );
		$checkfilename .= $destfilename_attributes;

		if ( $is_jpeg ) $checkfilename = preg_replace( '#.jpeg$#', '.jpg', $checkfilename );

		$uploads_dir = wp_upload_dir();
		$uploads_dir['basedir'] = preg_replace( '#\/\/#', '/', $uploads_dir['basedir'] );

		if ( null !== $destination_dir && '' != $destination_dir && apply_filters('realestate_enable_uploads_detection', true) ){
			$site_dir = trailingslashit( preg_replace( '#\/\/#', '/', $uploads_dir['basedir'] ) );
			$site_uri = trailingslashit( $uploads_dir['baseurl'] );
		}

		#check if we have an image with specified width and height

		if ( file_exists( $checkfilename ) ) return str_replace( $site_dir, trailingslashit( $site_uri ), $checkfilename );

		$size = @getimagesize( $localfile );
		if ( !$size ) return new WP_Error('invalid_image_path', __('Image doesn\'t exist'), $thumb);
		list($orig_width, $orig_height, $orig_type) = $size;

		#check if we're resizing the image to smaller dimensions
		if ( $orig_width > $new_width || $orig_height > $new_height ){
			if ( $orig_width < $new_width || $orig_height < $new_height ){
				#don't resize image if new dimensions > than its original ones
				if ( $orig_width < $new_width ) $new_width = $orig_width;
				if ( $orig_height < $new_height ) $new_height = $orig_height;

				#regenerate suffix and appended attributes in case we changed new width or new height dimensions
				$suffix = "{$add_to_suffix}{$new_width}x{$new_height}";
				$destfilename_attributes = '-' . $suffix . '.' . $ext;

				$checkfilename = ( '' != $destination_dir && null !== $destination_dir ) ? path_join( $destination_dir, $name ) : path_join( dirname( $localfile ), $name );
				$checkfilename .= $destfilename_attributes;

				#check if we have an image with new calculated width and height parameters
				if ( file_exists($checkfilename) ) return str_replace( $site_dir, trailingslashit( $site_uri ), $checkfilename );
			}

			#we didn't find the image in cache, resizing is done here
			$result = image_resize( $localfile, $new_width, $new_height, $crop, $suffix, $destination_dir );

			if ( !is_wp_error( $result ) ) {
				#transform local image path into URI

				if ( $is_jpeg ) $thumb = preg_replace( '#.jpeg$#', '.jpg', $thumb);

				$site_dir = str_replace( '\\', '/', $site_dir );
				$result = str_replace( '\\', '/', $result );
				$result = str_replace( '//', '/', $result );
				$result = str_replace( $site_dir, trailingslashit( $site_uri ), $result );
			}

			#returns resized image path or WP_Error ( if something went wrong during resizing )
			return $result;
		}

		#returns unmodified image, for example in case if the user is trying to resize 800x600px to 1920x1080px image
		return $thumb;
	}
}

add_action( 'pre_get_posts', 'realestate_custom_posts_per_page' );
function realestate_custom_posts_per_page( $query ) {
	global $shortname;

	if ( is_admin() ) return $query;

	if ( $query->is_category ) {
		$query->set( 'posts_per_page', realestate_get_option( $shortname . '_catnum_posts' ) );
	} elseif ( $query->is_tag ) {
		$query->set( 'posts_per_page', realestate_get_option( $shortname . '_tagnum_posts' ) );
	} elseif ( $query->is_search ) {
		if ( isset($_GET['realestate_searchform_submit']) ) {
			$postTypes = array();
			if ( !isset($_GET['realestate-inc-posts']) && !isset($_GET['realestate-inc-pages']) ) $postTypes = array('post');
			if ( isset($_GET['realestate-inc-pages']) ) $postTypes = array('page');
			if ( isset($_GET['realestate-inc-posts']) ) $postTypes[] = 'post';
			$query->set( 'post_type', $postTypes );

			if ( isset( $_GET['realestate-month-choice'] ) && $_GET['realestate-month-choice'] != 'no-choice' ) {
				$realestate_year = substr($_GET['realestate-month-choice'],0,4);
				$realestate_month = substr($_GET['realestate-month-choice'], 4, strlen($_GET['realestate-month-choice'])-4);

				$query->set( 'year', absint($realestate_year) );
				$query->set( 'monthnum', absint($realestate_month) );
			}

			if ( isset( $_GET['realestate-cat'] ) && $_GET['realestate-cat'] != 0 )
				$query->set( 'cat', absint($_GET['realestate-cat']) );
		}
		$query->set( 'posts_per_page', realestate_get_option( $shortname . '_searchnum_posts' ) );
	} elseif ( $query->is_archive ) {
		$query->set( 'posts_per_page', realestate_get_option( $shortname . '_archivenum_posts' ) );
	}

	return $query;
}

add_filter( 'default_hidden_meta_boxes', 'realestate_show_hidden_metaboxes', 10, 2 );
function realestate_show_hidden_metaboxes( $hidden, $screen ){
	# make custom fields and excerpt meta boxes show by default
	if ( 'post' == $screen->base || 'page' == $screen->base )
		$hidden = array('slugdiv', 'trackbacksdiv', 'commentstatusdiv', 'commentsdiv', 'authordiv', 'revisionsdiv');

	return $hidden;
}

add_filter('widget_title','realestate_widget_force_title');
function realestate_widget_force_title( $title ){
	#add an empty title for widgets ( otherwise it might break the sidebar layout )
	if ( $title == '' ) $title = ' ';

	return $title;
}

//modify the comment counts to only reflect the number of comments minus pings
if( version_compare( phpversion(), '4.4', '>=' ) ) add_filter('get_comments_number', 'realestate_comment_count', 0);
function realestate_comment_count( $count ) {
	if ( ! is_admin() ) {
		global $id;
		$get_comments = get_comments( array('post_id' => $id, 'status' => 'approve') );
		$comments_by_type = &separate_comments($get_comments);
		return count($comments_by_type['comment']);
	} else {
		return $count;
	}
}

add_action( 'admin_init', 'realestate_theme_check_clean_installation' );
function realestate_theme_check_clean_installation(){
	add_action( 'admin_notices', 'realestate_theme_epanel_reminder' );
}

if ( ! function_exists( 'realestate_theme_epanel_reminder' ) ){
	function realestate_theme_epanel_reminder(){
		global $shortname, $themename, $current_screen;

		if ( false === realestate_get_option( $shortname . '_logo' ) && 'appearance_page_core_functions' != $current_screen->id ){
			printf( __('<div class="updated"><p>This is a fresh installation of %1$s theme. Don\'t forget to go to <a href="%2$s">ePanel</a> to set it up. This message will disappear once you have clicked the Save button within the <a href="%2$s">theme\'s options page</a>.</p></div>',$themename), get_current_theme(), admin_url( 'themes.php?page=core_functions.php' ) );
		}
	}
}

?>